﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using ECatalog.Models.Kits;
using ECatalog.Models.UserModel;

namespace ECatalog.Controllers
{
    [Authorize]
    public class KitController : Controller
    {
        private readonly IKitRepository kitRepository;
        private readonly IKitBuilder kitBuilder;
        private readonly IUserRepository userRepository;

        public KitController(IKitRepository kitRepository, IKitBuilder kitBuilder, IUserRepository userRepository)
        {
            this.kitRepository = kitRepository;
            this.kitBuilder = kitBuilder;
            this.userRepository = userRepository;
        }


        [Authorize]
        public ActionResult Index()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var kits = kitRepository.GetAllKits();
            return View(kits);
        }

        [Authorize]
        [HttpPost]
        public ActionResult AddKit()
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var vm = kitBuilder.BuildVm(0, null);
            return View("Editor", vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult SaveKit(KitViewModel kitViewModel)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            if (ModelState.IsValid)
            {
                if (kitViewModel.KitId == 0)
                {
                    var k = kitBuilder.Build(kitViewModel);
                    var kit = kitRepository.AddKit(k);
                    if (kitViewModel.UploadedFile != null)
                    {
                        kitBuilder.SaveFile(kitViewModel.UploadedFile, kit);
                    }
                    return RedirectToAction("EditKit", "Kit", new { kit.KitId });
                }
                else
                {
                    var k = kitBuilder.Build(kitViewModel);
                    if (kitViewModel.UploadedFile != null)
                    {
                        kitBuilder.SaveFile(kitViewModel.UploadedFile, k);
                    }
                    kitBuilder.Save(k);
                    return RedirectToAction("EditKit", "Kit", new { k.KitId });
                }
            }

            return View("Editor");
        }

        [Authorize]
        [HttpPost]
        public ActionResult DeleteKit(int kitid)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            kitRepository.RemoveKitById(kitid);
            return RedirectToAction("Index");
        }

        [Authorize]
        [HttpPost]
        public RedirectToRouteResult DeleteKitModel(int modelid, int kitId)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            kitRepository.DeleteKitModel(modelid, kitId);
            return RedirectToAction("EditKit", "Kit", new { kitId });
        }

        public ActionResult EditKit(int kitid)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var m = kitRepository.GetKitById(kitid);
            var vm = kitBuilder.BuildVm(m.KitId, m.OrganizationId);
            return View("Editor", vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult SaveModelKit(int kitId)
        {
            @ViewBag.IsAdmin = userRepository.IsAdmin(User.Identity.Name);
            var ids =
                Request.Form.AllKeys.Where(k => k.Contains("ModelId="))
                       .Select(s => s.Split('='))
                       .Select(s => Convert.ToInt32(s[1])).ToArray();
            kitRepository.AddKitModels(ids, kitId);

            return RedirectToAction("EditKit", "Kit", new { kitId });
        }
    }
}
